package com.lzw.mall.admin.controller;
import com.lzw.mall.admin.service.IUserService;
import com.lzw.mall.admin.util.RequestUtil;
import com.lzw.mall.common.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/")
public class LoginController {


@Autowired
private IUserService userService;


    @GetMapping("/login")
    public String login() {
        return "login";
    }
    @PostMapping("/login")
    public String check(@RequestParam(value="username") String userName, String password) {

        System.out.println(userName);
        System.out.println(password);
        Boolean right = userService.check(userName, password);
        //redirect:重定向
        //forward：转发
        if (right) {
            return "redirect:/index";
        } else {
            //throw new BusinessException("用户名或密码错误") ;
            RequestUtil.getSession().setAttribute("msg", "用户名或密码错误");
            return "login";
            //return "forward:/login";
        }
    }
}
